<?php

namespace App\Models;

use App\Clients\RedBookClient;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class RedBookAccount extends Model
{

    protected $fillable = [
        'username',
        'password',
        'enable',
        'run_date',
        'run_log',
    ];

    public static function getAccountData($start, $end, $accounts = null)
    {
        if (!$accounts)
            $accounts = static::query()
                ->where('enable', true)
                ->get();

        foreach ($accounts as $account) {
            $client = new RedBookClient($account);
            $result = $client->getExcelClue($start, $end);
            $account->run_date = Carbon::now()->toDateTimeString();
            $account->run_log = $result ? '执行成功' : '执行失败';
            $account->save();
        }

    }

    public static function getTodayData()
    {
        $date = Carbon::today()->toDateString();
        static::getAccountData($date, $date);
    }

    public static function getYesterdayData()
    {
        $date = Carbon::yesterday()->toDateString();
        static::getAccountData($date, $date);
    }
    
}
